package com.syqc.adapter

import android.view.View
import android.widget.ImageView
import androidx.activity.viewModels
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.autoDispose
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.choose.car.ChooseCarActivity
import com.syqc.choose.teamcar.TeamsCarsActivity
import com.syqc.comlib.base.BaseBarActivity
import com.syqc.comlib.rx.filterFast
import com.syqc.comlib.utils.ActivityResultUtil.launchAct
import com.syqc.comlib.utils.ActivityResultUtil.registerResultOk
import com.syqc.comlib.utils.ToolbarHelper
import com.syqc.comlib.utils.loadStringDImgError
import com.syqc.custom.CarInfoTextView
import com.syqc.entity.Driver
import com.syqc.entity.KeyValue
import com.syqc.entity.OnlyCarInfo
import com.syqc.monitor.R
import com.syqc.monitor.databinding.ActivityOnlyCarInfoBinding
import com.syqc.monitor.popup.DriversPopup
import com.syqc.monitor.ui.carinfo.OnlyCarVM
import com.syqc.monitor.ui.newaddcar.PhotoViewPopup
import com.syqc.utils.Keys
import com.syqc.utils.NetDataUtil
import com.syqc.utils.TimeUtil
import com.syqc.utils.ToolsUtil

class OnlyCarInfoActivity :
    BaseBarActivity<ActivityOnlyCarInfoBinding>(ActivityOnlyCarInfoBinding::inflate) {

    private val infoAdapter by lazy { OnlyCarInfoAdapter() }
    private val carAdapter by lazy { PhotosAdapter() }
    private val mPhotoView by lazy { PhotoViewPopup(this) }
    private val mViewModel by viewModels<OnlyCarVM>()
    private val mDriversPopup by lazy { DriversPopup(this) }

    private val tmnAdapter by lazy { TmnInfoAdapter() }
    private val tmnPhotoAdapter by lazy { PhotosAdapter() }

    private val ownerAdapter by lazy {
        OwnerAdapter(driverPhone = {
            ToolsUtil.talkPhone(this, mViewModel.firstPhone)
        }, showDrivers = {
            mDriversPopup.show(window.decorView, mViewModel.driverInfo)
        })
    }

    override fun initView() {
        ToolbarHelper.setToolbar(this, R.string.title_other_car_info)
        binding.rvCarInfo.layoutManager = LinearLayoutManager(this)
        binding.rvCarInfo.adapter = infoAdapter

        binding.rvCarPhoto.layoutManager = GridLayoutManager(this, 4)
        binding.rvCarPhoto.adapter = carAdapter

        binding.rvOwnerDriver.layoutManager = LinearLayoutManager(this)
        binding.rvOwnerDriver.adapter = ownerAdapter

        binding.rvTmnInfo.layoutManager = LinearLayoutManager(this)
        binding.rvTmnInfo.adapter = tmnAdapter

        binding.rvTmnPhoto.layoutManager = GridLayoutManager(this, 4)
        binding.rvTmnPhoto.adapter = tmnPhotoAdapter

        initData()
        if (NetDataUtil.isSyncTree) {
            carLauncher.launchAct<TeamsCarsActivity>(this)
        } else {
            carLauncher.launchAct<ChooseCarActivity>(this)
        }
    }

    fun initData() {
        mViewModel.toastObserver().autoDispose(scopeProvider).subscribe(this::toastObserver)

        mViewModel.carLive.observe(this) {
            infoAdapter.setList(it)
            binding.tvCarInfo.isVisible = true
            binding.nsvInfo.isVisible = true
            binding.rdbLocation.isVisible = mViewModel.mainTmn != null
            binding.rdbVideo.isVisible = mViewModel.videoTmn != null
            binding.rdbAdas.isVisible = mViewModel.adasTmn != null
            val isTmnShow =
                !(mViewModel.mainTmn == null && mViewModel.videoTmn == null && mViewModel.adasTmn == null)

            binding.rdgTmn.isVisible = isTmnShow
            binding.rvTmnInfo.isVisible = isTmnShow
            binding.tvOwner.isVisible = isTmnShow
            binding.tvTmn.isVisible = isTmnShow
            binding.rvTmnPhoto.isVisible = isTmnShow
            if (mViewModel.mainTmn != null) {
                tmnAdapter.setList(mViewModel.mainTmn?.keyValues)
                val picMap = mViewModel.mainTmn?.picMap
                tmnPhotoAdapter.setList(picMap)
                binding.rdbLocation.isChecked = true
                binding.tvTmn.isVisible = !picMap.isNullOrEmpty()
            } else if (mViewModel.videoTmn != null) {
                tmnAdapter.setList(mViewModel.videoTmn?.keyValues)
                val picMap = mViewModel.videoTmn?.picMap
                tmnPhotoAdapter.setList(picMap)
                binding.rdbVideo.isChecked = true
                binding.tvTmn.isVisible = !picMap.isNullOrEmpty()
            } else if (mViewModel.adasTmn != null) {
                tmnAdapter.setList(mViewModel.adasTmn?.keyValues)
                val picMap = mViewModel.adasTmn?.picMap
                tmnPhotoAdapter.setList(picMap)
                binding.rdbAdas.isChecked = true
                binding.tvTmn.isVisible = !picMap.isNullOrEmpty()
            }

            ownerAdapter.driverInfo = mViewModel.driverInfo
        }

        mViewModel.carPhotos.observe(this) {
            binding.tvCarPhoto.isGone = it.isNullOrEmpty()
            binding.rvCarPhoto.isGone = it.isNullOrEmpty()
            carAdapter.setList(it)
        }

        mViewModel.ownerLive.observe(this) {
            ownerAdapter.setList(it)
        }
    }

    override fun setListener() {
        binding.lrvChooseCar.filterFast {
            if (NetDataUtil.isSyncTree) {
                carLauncher.launchAct<TeamsCarsActivity>(this)
            } else {

                carLauncher.launchAct<ChooseCarActivity>(this)
            }
        }
        binding.rdbLocation.filterFast {
            tmnAdapter.setList(mViewModel.mainTmn?.keyValues)
            val picMap = mViewModel.mainTmn?.picMap
            tmnPhotoAdapter.setList(picMap)
            binding.tvTmn.isVisible = !picMap.isNullOrEmpty()
        }
        binding.rdbVideo.filterFast {
            tmnAdapter.setList(mViewModel.videoTmn?.keyValues)
            val picMap = mViewModel.videoTmn?.picMap
            tmnPhotoAdapter.setList(picMap)
            binding.tvTmn.isVisible = !picMap.isNullOrEmpty()
        }

        binding.rdbAdas.filterFast {
            tmnAdapter.setList(mViewModel.adasTmn?.keyValues)
            val picMap = mViewModel.adasTmn?.picMap
            tmnPhotoAdapter.setList(picMap)
            binding.tvTmn.isVisible = !picMap.isNullOrEmpty()
        }

        carAdapter.setOnItemClickListener { adapter, view, position ->
            mPhotoView.show(carAdapter.data[position])
        }
        tmnPhotoAdapter.setOnItemClickListener { adapter, view, position ->
            mPhotoView.show(tmnPhotoAdapter.data[position])
        }
    }

    private val carLauncher = registerResultOk { result ->
        result.data?.let {
            val carId = it.getStringExtra(Keys.INTENT_CAR_ID) ?: ""
            val carName = it.getStringExtra(Keys.INTENT_CAR_NAME)
            binding.lrvChooseCar.setRightText(carName ?: "")
            mViewModel.carId = carId
            mViewModel.getCarInfo()
        }
    }
}

class LocationCarAdapter :
    BaseQuickAdapter<OnlyCarInfo, BaseViewHolder>(R.layout.item_car_info) {
    override fun convert(holder: BaseViewHolder, item: OnlyCarInfo) {
        val citvInfo = holder.getView<CarInfoTextView>(R.id.citvInfo)
        citvInfo.setRightText(item.value)
        citvInfo.setLeftTitle(item.type)
        when (holder.layoutPosition) {
            1, 2, 6 -> citvInfo.setEndGone()
        }
    }
}

class OnlyCarInfoAdapter :
    BaseQuickAdapter<OnlyCarInfo, BaseViewHolder>(R.layout.item_car_info) {
    override fun convert(holder: BaseViewHolder, item: OnlyCarInfo) {
        val citvInfo = holder.getView<CarInfoTextView>(R.id.citvInfo)
        citvInfo.setRightText(item.value)
        citvInfo.setLeftTitle(item.type)
        if (holder.layoutPosition == 6) {
            if (item.value.isNotEmpty()) {
                val isOver = TimeUtil.instance.isSimpleOverNow(item.value)
                citvInfo.setRightTextColor(if (isOver) R.color.color_00F else R.color.color_RED)
            }
            citvInfo.setRightWeight(2f)
        } else {
            citvInfo.setRightTextColor(R.color.color_666)
            citvInfo.setRightWeight(3f)
        }
    }
}

class OwnerAdapter(
    private val showDrivers: () -> Unit,
    private val driverPhone: () -> Unit
) : BaseQuickAdapter<OnlyCarInfo, BaseViewHolder>(R.layout.item_car_info) {
    var driverInfo = mutableListOf<Driver>()
    override fun convert(holder: BaseViewHolder, item: OnlyCarInfo) {
        val citvInfo = holder.getView<CarInfoTextView>(R.id.citvInfo)
        citvInfo.setRightText(item.value)
        citvInfo.setLeftTitle(item.type)
        citvInfo.setRightTextColor(R.color.color_666)
        citvInfo.setRightWeight(3f)
        when (holder.layoutPosition) {
            2 -> {
                citvInfo.setNextInVisible(driverInfo.size > 1)
                citvInfo.setOnNextListener {
                    showDrivers()
                }
                citvInfo.setOnPhoneListener {
                    driverPhone()
                }
                val firstPhone =
                    if (driverInfo.isEmpty()) ""
                    else driverInfo[0].driverPhone ?: ""
                citvInfo.setPhoneInVisible(firstPhone.isNotEmpty())
                citvInfo.setRightWeight(3f)
            }

            1 -> {
                citvInfo.setNextInVisible(false)
                citvInfo.setPhoneInVisible(item.value.isNotEmpty())
                citvInfo.setOnPhoneListener {
                    ToolsUtil.talkPhone(context, item.value)
                }
                citvInfo.setRightWeight(3f)
            }

            else -> {
                citvInfo.setRightTextColor(R.color.color_666)
                citvInfo.setRightWeight(3f)
            }
        }
    }
}

class TmnInfoAdapter :
    BaseQuickAdapter<KeyValue, BaseViewHolder>(R.layout.item_car_info) {
    override fun convert(holder: BaseViewHolder, item: KeyValue) {
        val citvInfo = holder.getView<CarInfoTextView>(R.id.citvInfo)
        citvInfo.setRightText(item.value)
        citvInfo.setLeftTitle(item.key)
        val index = if (data.size > 7) 7 else 6
        if (holder.layoutPosition == index) {
            if (item.value.isNotEmpty()) {
                val isOver = TimeUtil.instance.isAllOverNow(item.value)
                citvInfo.setRightTextColor(if (isOver) R.color.color_00F else R.color.color_RED)
            }
            citvInfo.setRightWeight(2f)
        } else {
            citvInfo.setRightTextColor(R.color.color_666)
            citvInfo.setRightWeight(3f)
        }
    }
}


class PhotosAdapter : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_car_tmn_photo) {
    override fun convert(holder: BaseViewHolder, item: String) {
        holder.getView<ImageView>(R.id.iv_choose_photo).loadStringDImgError(context, item)

        holder.getView<View>(R.id.iv_choose_delete).isVisible = false
    }
}